package com.coocaa.ops.admin.v1.auth.sys.biz.impl;

import com.coocaa.ops.admin.v1.auth.sys.biz.ISysUserBiz;
import com.coocaa.ops.admin.v1.auth.sys.mapper.ISysUserMapper;
import com.coocaa.ops.admin.v1.dict.biz.DictInfoBizImpl;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.dict.entity.DictInfoEntity;
import com.coocaa.ops.common.tool.date.DateUtil;
import com.coocaa.ops.common.tool.md5.MD5Util;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2019/3/11.
 * @description: 系统用户 biz 实现
 *
 */
@Service
public class SysUserBizImpl extends BaseBizImpl<ISysUserMapper, SysUserEntity> implements ISysUserBiz {

    @Autowired
    private DictInfoBizImpl dictInfoBiz;

    @Override
    public SysUserEntity updateById(SysUserEntity entity) {
        entity.setLastUpdateDate(DateUtil.getCurDate());
        return super.updateById(entity);
    }

    @Override
    public void updatePassword(String oldPassword, SysUserEntity user, String newPassword) throws Exception {
        SysUserEntity update = new SysUserEntity();
        update.setId(user.getId());
        update.setLoginPwd(MD5Util.md5(MD5Util.md5(newPassword + user.getSalt())));
        update.setLastUpdateDate(DateUtil.getCurDate());
        updateById(update);
    }

    /**
     * 获取用户对应的业务分类权限
     *
     * @param userId
     * @return
     */
    @Override
    public Integer[] listUserBusinessType(Integer userId) {
        List<DictInfoEntity> types = dictInfoBiz.listUserBusinessType(userId);
        if (CollectionUtils.isNotEmpty(types)) {
            //存储业务组id
            Integer[] dict = new Integer[types.size()];
            for (int i = 0; i < types.size(); i++) {
                dict[i] = types.get(i).getId();
            }
            return dict;
        }
        return new Integer[0];
    }

    @Override
    public SysUserEntity getUserByLoginName(String loginName) {
        QueryTemplate<SysUserEntity> wrapper = QueryTemplate.getEqQueryTemplate("login_name", loginName);
        wrapper.eq("disabled", 0);
        return getOneSysUser(wrapper);
    }

    @Override
    public SysUserEntity getUserByEmail(String email) {
        QueryTemplate<SysUserEntity> wrapper = QueryTemplate.getEqQueryTemplate("email", email);
        wrapper.eq("disabled", 0);
        return getOneSysUser(wrapper);
    }

    @Override
    public Integer getUserCountByDept(Integer deptId) {
        QueryTemplate<SysUserEntity> wrapper = new QueryTemplate<>();
        wrapper.lambda().eq(SysUserEntity::getDisabled, 0)
                .eq(SysUserEntity::getDeptId, deptId);
        return getMapper().selectCount(wrapper);
    }

    @Override
    public String getEmailByUserId(Integer userId) {
        SysUserEntity user = queryByUserId(userId);
        return null != user ? user.getEmail() : null;
    }

    @Override
    public String getLoginName(Integer userId) {
        SysUserEntity user = queryByUserId(userId);
        return null != user ? user.getLoginName() : null;
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private SysUserEntity getOneSysUser(QueryTemplate wrapper) {
        List<SysUserEntity> list = getMapper().selectList(wrapper);
        SysUserEntity user = CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
        if (null != user) {
            user.setRoles(getMapper().listRoleIdByUserId(user.getId()));
        }
        return user;
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId
     * @return
     */
    private SysUserEntity queryByUserId(Integer userId) {
        QueryTemplate<SysUserEntity> wrapper = new QueryTemplate<>();
        wrapper.lambda().eq(SysUserEntity::getId, userId);
        List<SysUserEntity> list = getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }
}
